package com.hitqz.robot.dispatch.netty.service.handle.impl;

import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.cache.Cache;
import com.google.common.collect.Maps;
import com.google.common.util.concurrent.RateLimiter;
import com.hitqz.robot.api.business.dto.SubscriptTopicDto;
import com.hitqz.robot.api.business.entity.JobModeEntity;
import com.hitqz.robot.api.business.entity.PathResourceEntity;
import com.hitqz.robot.api.business.enums.WorkStatusEnum;
import com.hitqz.robot.api.business.model.dto.FlowPatrolPreAndEndTicketsDto;
import com.hitqz.robot.api.common.dto.info.RobotNavInfoDto;
import com.hitqz.robot.api.common.entity.RobotMapPos;
import com.hitqz.robot.api.dispatch.protocol.dto.RobotApplyGotoTargetDto;
import com.hitqz.robot.api.dispatch.protocol.function.CheckOperationFunction;
import com.hitqz.robot.biz.mapper.PathResourceMapper;
import com.hitqz.robot.dispatch.netty.service.handle.bo.HandleAgvBlockDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;


/**
 * @author kehanjiang
 * @date 2023/11/03
 */
@Slf4j
@Component
public class LunzhouRuleHandle extends AbstractRuleHandle {
    private final static RateLimiter logLimiter = RateLimiter.create(1);

    //缓存架区域点位集合
    private static List<String> goodBufferPosList = Arrays.asList(
            "LM118", "LM117", "LM116", "LM115", "LM114", "LM113", "LM112", "LM111",
            "LM128", "LM127", "LM126", "LM125", "LM124", "LM123", "LM122", "LM121",
            "LM138", "LM137", "LM136", "LM135", "LM134", "LM133", "LM132", "LM131",
            "LM148", "LM147", "LM146", "LM145", "LM144", "LM143", "LM142", "LM141",
            "LM158", "LM157", "LM156", "LM155", "LM154", "LM153", "LM152", "LM151",
            "LM168", "LM167", "LM166", "LM165", "LM164", "LM163", "LM162", "LM161",
            "LM178", "LM177", "LM176", "LM175", "LM174", "LM173", "LM172", "LM171",
            "LM188", "LM187", "LM186", "LM185", "LM184", "LM183", "LM182", "LM181"
    );


    @Override
    public String getType() {
        return "lunzhou";
    }

    /**
     * 生成 前往任务
     *
     * @param dto
     */
    @Override
    public void applyGotoTarget(RobotApplyGotoTargetDto dto) {
        String clientId = dto.getClientId();
        String currentPos = dto.getCurrentPos();
        String targetPos = dto.getTargetPos();
        LinkedHashMap<String, CheckOperationFunction> middlePos = Optional.ofNullable(dto.getMiddlePos())
                .orElse(new LinkedHashMap<>());
//        this.cmdRobot2Pos(clientId, targetPos);
        this.submitTask(clientId, currentPos, middlePos, targetPos);
    }

    /**
     * 提交前往任务
     *
     * @param clientId
     * @param currentPos   当前点位
     * @param middlePosMap 中转点位
     * @param targetPos    目标点位
     */
    private void submitTask(String clientId, String currentPos, LinkedHashMap<String, CheckOperationFunction> middlePosMap, String targetPos) {
        FutureTask<String> futureTask = new FutureTask<>(() -> {
            try {
                LinkedHashMap<String, CheckOperationFunction> allPosMap = Maps.newLinkedHashMap(middlePosMap);
                allPosMap.put(targetPos, null);
                for (Map.Entry<String, CheckOperationFunction> posEntry : allPosMap.entrySet()) {
                    String target = posEntry.getKey();
                    String current = this.getCurrentAgvPos(clientId);
                    List<RobotMapPos> navPath = getNavPath(getRoundRobinUrl(), current, target);
                    log.warn("[{}]获取到的navPath{}", clientId, navPath.stream().map(RobotMapPos::getCode).toList());
                    //计算出所需占用的路径资源
                    List<PathResourceEntity> allPathResourceList = pathResourceMapper.selectList(null);
                    List<PathResourceEntity> needPathResourceList = new ArrayList<>();
                    for (RobotMapPos robotMapPos : navPath) {
                        String code = robotMapPos.getCode();
                        for (PathResourceEntity pathResourceEntity : allPathResourceList) {
                            List<String> posList = List.of(pathResourceEntity.getPathList().split(","));
                            if (posList.contains(code) && !needPathResourceList.contains(pathResourceEntity)) {
                                needPathResourceList.add(pathResourceEntity);
                            }
                        }
                    }
                    log.warn("[{}]获取到的needPathResourceList{}", clientId, needPathResourceList.stream().map(PathResourceEntity::getId).toList());
                    Long lastResourceId = null;
                    //资源申请，申请到就走，申请不到就等待
                    for (PathResourceEntity pathResourceEntity : needPathResourceList) {
                        log.warn("[{}]==>即将申请的资源{}", clientId, pathResourceEntity.getPathList());
                        Long resourceId = pathResourceEntity.getId();
                        //申请资源,申请不到就等待
                        this.applyResource(clientId, resourceId);
                        //申请成功，走到资源允许范围内 离目标点位最近的点，同时标记上一个资源已完成占用 状态改为2
                        this.goTargetInResourceRange(clientId, navPath, resourceId, lastResourceId);
                        lastResourceId = resourceId;
                    }
                    // 轮对前往中间点位是为了能走空车道，到了中间点位后可以直接继续前往目标点位
                    // 轮轴前往中间点位是为了避让其他AGV，到了中间点位后需要满足一定条件才能继续前往下个点位
                    CheckOperationFunction checkOperationFunction = posEntry.getValue();
                    if (checkOperationFunction != null) {
                        checkOperationFunction.waitUtilRelease();
                    }
                }
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }
            return "";
        });
        threadPoolExecutor.execute(futureTask);
        List<FutureTask> futureTaskList = futureTaskMap.computeIfAbsent(clientId, k -> new ArrayList<>());
        futureTaskList.add(futureTask);
    }

    /**
     * 是否需要重新申请路径(可能需要前往现有路径的其他点位上避让一下高优先级任务)
     * <p>
     * 轮对不需避让
     *
     * @param clientId
     * @param resourceId
     * @return
     */
    private boolean changePath(String clientId, Long resourceId) throws Exception {
        return false;
    }

    /**
     * 申请成功，走到资源允许范围内 离目标点位最近的点，同时标记上一个资源已完成占用 状态改为2
     *
     * @param clientId
     * @param navPath
     * @param resourceId
     * @param lastResourceId
     */
    private void goTargetInResourceRange(String clientId, List<RobotMapPos> navPath, Long resourceId, Long lastResourceId) throws Exception {
        List<String> navPathCode = navPath.stream().map(RobotMapPos::getCode).collect(Collectors.toList());
        PathResourceEntity pathResourceEntity = pathResourceMapper.selectById(resourceId);
        List<String> posList = List.of(pathResourceEntity.getPathList().split(","));
        Map<String, Integer> posDisMap = new HashMap<>();
        for (String pos : posList) {
            int index = navPathCode.indexOf(pos);
            posDisMap.put(pos, index);
        }
        // 找到资源内离目标点位最近的点
        Map.Entry<String, Integer> maxEntry = posDisMap.entrySet()
                .stream()
                .max(Map.Entry.comparingByValue()).orElse(null);
        String targetPos = maxEntry.getKey();
        log.info("[{}] 资源内：{} 离目标点位:{} 最近的点是:{} ", clientId, posList,
                navPathCode.get(navPathCode.size() - 1), targetPos);
        // 前往目标点位
        this.cmdRobot2Pos(clientId, targetPos);

        String current = this.getCurrentAgvPos(clientId);
        //标记上个资源已占用结束
        if (lastResourceId != null) {
            PathResourceEntity lastPathResourceEntity = pathResourceMapper.selectById(lastResourceId);
            List<String> lastPosCode = List.of(lastPathResourceEntity.getPathList().split(","));
            while (lastPosCode.contains(current) || !StringUtils.hasText(current)) {
                current = this.getCurrentAgvPos(clientId);
                TimeUnit.SECONDS.sleep(1);
            }
            log.info("[{}] 当前位置：{}，已离开资源{}，开始标记资源占用结束！", clientId, current, lastPosCode);
            int updateCnt = pathResourceMapper.update(Wrappers.<PathResourceEntity>lambdaUpdate()
                    .set(PathResourceEntity::getStatus, "2")
                    .eq(PathResourceEntity::getRobotSn, clientId)
                    .eq(PathResourceEntity::getId, lastResourceId)
            );
            log.info("[{}] 更新{}条数据", clientId, updateCnt);
        }

        //等待AGV到达目标点位
        while (!targetPos.equals(current)) {
            current = this.getCurrentAgvPos(clientId);
            TimeUnit.SECONDS.sleep(1);
        }
        log.info("[{}] 达到位置:{} ", clientId, targetPos);
    }

    /**
     * 申请资源,申请不到就等待
     *
     * @param clientId
     * @param resourceId
     * @throws Exception
     */
    private void applyResource(String clientId, Long resourceId) throws Exception {
        try {
            //校验 阻塞 直到允许申请到资源
            while (!(this.checkOtherAgvTask(clientId, resourceId) && this.allowApplyResource(clientId, resourceId))) {
                blockForApplyResource.put(clientId, resourceId);
                TimeUnit.SECONDS.sleep(1);
                if (logLimiter.tryAcquire(5)) {
                    PathResourceEntity resource = pathResourceMapper.selectById(resourceId);
                    log.warn("[{}] 申请不到资源 {}", clientId, resource.getPathList());
                }
            }

            pathResourceMapper.update(Wrappers.<PathResourceEntity>lambdaUpdate()
                    .set(PathResourceEntity::getStatus, "1")
                    .set(PathResourceEntity::getRobotSn, clientId)
                    .eq(PathResourceEntity::getId, resourceId)
            );
        } finally {
            blockForApplyResource.put(clientId, -1L);
        }
    }


    private String getOtherTargetPos(RobotMapPos targetPos) {
        if (targetPos == null) return "";
        return targetPos.getCode();
    }

    /**
     * 根据其他AGV任务及位置 判断是否允许申请资源
     *
     * @param clientId
     * @param resourceId
     * @return
     */
    private boolean checkOtherAgvTask(String clientId, Long resourceId) throws Exception {
        RobotNavInfoDto currentAgvDto = this.getCurrentAgvDto(clientId);
        RobotNavInfoDto otherAgvDto = this.getOtherAgvDto(clientId);

        if (currentAgvDto == null || otherAgvDto == null) return true;
        String currentPos = this.getCurrentAgvPos(clientId);
        String otherPos = this.getOtherAgvPos(clientId);

        String otherTaskParams = otherAgvDto.getTaskParams();
        if (!StringUtils.hasText(otherTaskParams)) return true;
        FlowPatrolPreAndEndTicketsDto otherDto = JSONObject.parseObject(otherTaskParams, FlowPatrolPreAndEndTicketsDto.class);
        if (otherDto == null) return true;
        String otherSourcePosCode = Optional.ofNullable(otherDto.getModalData().get("sourcePosName"))
                .orElse(otherDto.getModalData().get("sourcePosCode")).toString();
        String otherTargetPosCode = Optional.ofNullable(otherDto.getModalData().get("targetPosName"))
                .orElse(otherDto.getModalData().get("targetPosCode")).toString();

        log.info("[{}] otherSourcePosCode:{} otherTargetPosCode:{}", clientId, otherSourcePosCode, otherTargetPosCode);

        String currTaskParams = currentAgvDto.getTaskParams();
        if (!StringUtils.hasText(currTaskParams)) return true;
        FlowPatrolPreAndEndTicketsDto currDto = JSONObject.parseObject(currTaskParams, FlowPatrolPreAndEndTicketsDto.class);
        if (currDto == null) return true;
        String currSourcePosCode = Optional.ofNullable(currDto.getModalData().get("sourcePosName"))
                .orElse(currDto.getModalData().get("sourcePosCode")).toString();
        String currTargetPosCode = Optional.ofNullable(currDto.getModalData().get("targetPosName"))
                .orElse(currDto.getModalData().get("targetPosCode")).toString();
        log.info("[{}] currSourcePosCode:{} currTargetPosCode:{}", clientId, currSourcePosCode, currTargetPosCode);
        return true;
    }


    /**
     * 判断是否允许申请资源 （就算资源空闲也要判断能否申请）
     *
     * @param clientId
     * @param resourceId
     * @return
     */
    private boolean allowApplyResource(String clientId, Long resourceId) throws Exception {
        PathResourceEntity resource = pathResourceMapper.selectById(resourceId);
        if (!(
                ("0".equals(resource.getStatus()) && !StringUtils.hasText(resource.getRobotSn()))
                        || (!"0".equals(resource.getStatus()) && clientId.equals(resource.getRobotSn()))
        )) {
            return false;
        }

        RobotNavInfoDto currentAgvDto = this.getCurrentAgvDto(clientId);
        RobotNavInfoDto otherAgvDto = this.getOtherAgvDto(clientId);
        if (currentAgvDto == null || otherAgvDto == null) return false;
        if (resourceId.intValue() == 44) {//LM217,LM219
            String currentPos = this.getCurrentAgvPos(clientId);
            String otherPos = this.getOtherAgvPos(clientId);
            String currentTargetPos = currentAgvDto.getTargetPos().getCode();
            String otherTargetPos = this.getOtherTargetPos(otherAgvDto.getTargetPos());
            if ("LM214".equals(currentPos)) {
                //其他AGV在LM247,LM220,LM230 ,且其他AGV目标点位是LM217 则让其他AGV先申请当前资源
                List<String> posList = List.of("LM247", "LM220", "LM230");
                if (posList.contains(otherPos) && "LM217".equals(otherTargetPos)) {
                    return false;
                }
            }
        }

        if (resourceId.intValue() == 46) {//LM247,LM220,LM230
            String currentPos = this.getCurrentAgvPos(clientId);
            String otherPos = this.getOtherAgvPos(clientId);
            String currentTargetPos = currentAgvDto.getTargetPos().getCode();
            String otherTargetPos = this.getOtherTargetPos(otherAgvDto.getTargetPos());
            if ("LM55".equals(currentPos)) {
                List<String> posList = List.of("LM217", "LM219", "LM214");
                //其他AGV在LM217,LM219,LM214 ,且其他AGV目标点位不是LM214,LM217(说明在返回)
                // 则让其他AGV先申请当前资源
                if (posList.contains(otherPos) && !List.of("LM214", "LM217").equals(otherTargetPos)) {
                    return false;
                }
                //其他AGV在LM217,LM219,LM214 ，且其他AGV目标点位是 LM217 (说明正前往LM217)
                if (posList.contains(otherPos) && "LM217".equals(otherTargetPos)) {
                    return false;
                }
                //其他AGV在LM217,LM219,LM214 ，且其他AGV目标点位是 LM214,且当前AGV目标点位是LM214 (同目标)
                if (posList.contains(otherPos) && "LM214".equals(otherTargetPos) && "LM214".equals(currentTargetPos)) {
                    return false;
                }
            }
        }

        if (resourceId.intValue() == 64) {//LM231,LM232,LM248
            String currentPos = this.getCurrentAgvPos(clientId);
            String otherPos = this.getOtherAgvPos(clientId);
            String currentTargetPos = currentAgvDto.getTargetPos().getCode();
            String otherTargetPos = this.getOtherTargetPos(otherAgvDto.getTargetPos());
            if ("LM55".equals(currentPos)) {
                List<String> posList = List.of("LM249", "LM234", "LM233", "LM222", "LM190");
                //其他AGV在LM249,LM234,LM233,LM222,LM190 ,且其他AGV目标点位是LM55 则让其他AGV先申请当前资源
                if (posList.contains(otherPos) && "LM55".equals(otherTargetPos)) {
                    return false;
                }
            }
        }

        if (resourceId.intValue() == 66) {//LM249,LM234,LM233,LM222,LM190
            String currentPos = this.getCurrentAgvPos(clientId);
            String otherPos = this.getOtherAgvPos(clientId);
            String currentTargetPos = currentAgvDto.getTargetPos().getCode();
            String otherTargetPos = this.getOtherTargetPos(otherAgvDto.getTargetPos());
            if ("LM33".equals(currentPos)) {
                List<String> posList = List.of("LM211", "LM55", "LM231", "LM232", "LM248");
                //其他AGV在LM211,LM55,LM231,LM232,LM248 ,且其他AGV目标点位不是LM55(说明在返回)
                // 则让其他AGV先申请当前资源
                if (posList.contains(otherPos) && !List.of("LM55").equals(otherTargetPos)) {
                    return false;
                }
            }
        }

        if (resourceId.intValue() == 68) {//LM211,LM55
            String currentPos = this.getCurrentAgvPos(clientId);
            String otherPos = this.getOtherAgvPos(clientId);
            String currentTargetPos = currentAgvDto.getTargetPos().getCode();
            String otherTargetPos = this.getOtherTargetPos(otherAgvDto.getTargetPos());
            if ("LM250".equals(currentPos)) {
                List<String> posList = List.of("LM231", "LM232", "LM248", "LM249", "LM234", "LM233", "LM222", "LM190");
                //其他AGV在LM231,LM232,LM248,LM249,LM234,LM233,LM222,LM190 ,且其他AGV目标点位是LM214,LM217 则让其他AGV先申请当前资源
                if (posList.contains(otherPos) && List.of("LM214", "LM217").contains(otherTargetPos)) {
                    return false;
                }
            }
        }


        if (resourceId.intValue() == 84) {//LM202,LM210,LM236,LM29,LM235,LM212,LM28,LM54
            String currentPos = this.getCurrentAgvPos(clientId);
            String otherPos = this.getOtherAgvPos(clientId);
            String currentTargetPos = currentAgvDto.getTargetPos().getCode();
            String otherTargetPos = this.getOtherTargetPos(otherAgvDto.getTargetPos());
            if ("LM31".equals(currentPos)) {
                List<String> posList = List.of("LM5", "LM200", "LM201");
                //其他AGV在LM5,LM200,LM201 ,且当前AGV目标点位是LM29 则让其他AGV先申请当前资源
                if (posList.contains(otherPos) && "LM29".equals(currentTargetPos)) {
                    return false;
                }
                posList = List.of("LM203", "LM207", "LM208", "LM209", "LM218", "LM57",
                        "LM242", "LM243", "LM244", "LM245", "LM246", "LM61", "LM60", "LM215", "LM216", "LM58", "LM225");
                //其他AGV在资源24(LM203,LM207,LM208,LM209,LM218,LM57,LM242,LM243,LM244,LM245,LM246,LM61,LM60,LM215,LM216,LM58,LM225)
                //,且当前AGV目标点位是LM57 则让其他AGV先申请当前资源
                if (posList.contains(otherPos) && "LM57".equals(currentTargetPos)) {
                    return false;
                }
            }
        }
        //LM33,LM52,LM51,LM50,LM49,LM48,LM37,LM32,
        // LM113,LM112,LM111,LM123,LM122,LM121,LM133,LM132,LM131,LM143,LM142,LM141,
        // LM153,LM152,LM151,LM163,LM162,LM161,LM173,LM172,LM171,LM183,LM182,LM181

        if (resourceId.intValue() == 94) {
            String currentPos = this.getCurrentAgvPos(clientId);
            String otherPos = this.getOtherAgvPos(clientId);
            String currentTargetPos = currentAgvDto.getTargetPos().getCode();
            String otherTargetPos = this.getOtherTargetPos(otherAgvDto.getTargetPos());
            if (List.of("LM190", "LM31").contains(currentPos)) {
                List<String> posList = List.of("LM118", "LM117", "LM116", "LM115", "LM114");
                //其他AGV在LM118,LM117,LM116,LM115,LM114 ,且当前AGV目标点位是其他AGV所在点位 则让其他AGV先申请当前资源
                if (posList.contains(otherPos) && posList.contains(currentTargetPos)) {
                    return false;
                }
                posList = List.of("LM128", "LM127", "LM126", "LM125", "LM124");
                //其他AGV在LM128,LM127,LM126,LM125,LM124 ,且当前AGV目标点位是其他AGV所在点位 则让其他AGV先申请当前资源
                if (posList.contains(otherPos) && posList.contains(currentTargetPos)) {
                    return false;
                }
                posList = List.of("LM138", "LM137", "LM136", "LM135", "LM134");
                //其他AGV在LM138,LM137,LM136,LM135,LM134 ,且当前AGV目标点位是其他AGV所在点位 则让其他AGV先申请当前资源
                if (posList.contains(otherPos) && posList.contains(currentTargetPos)) {
                    return false;
                }
                posList = List.of("LM148", "LM147", "LM146", "LM145", "LM144");
                //其他AGV在LM148,LM147,LM146,LM145,LM144 ,且当前AGV目标点位是其他AGV所在点位 则让其他AGV先申请当前资源
                if (posList.contains(otherPos) && posList.contains(currentTargetPos)) {
                    return false;
                }
                posList = List.of("LM158", "LM157", "LM156", "LM155", "LM154");
                //其他AGV在LM158,LM157,LM156,LM155,LM154 ,且当前AGV目标点位是其他AGV所在点位 则让其他AGV先申请当前资源
                if (posList.contains(otherPos) && posList.contains(currentTargetPos)) {
                    return false;
                }
                posList = List.of("LM168", "LM167", "LM166", "LM165", "LM164");
                //其他AGV在LM168,LM167,LM166,LM165,LM164 ,且当前AGV目标点位是其他AGV所在点位 则让其他AGV先申请当前资源
                if (posList.contains(otherPos) && posList.contains(currentTargetPos)) {
                    return false;
                }
                posList = List.of("LM178", "LM177", "LM176", "LM175", "LM174");
                //其他AGV在LM178,LM177,LM176,LM175,LM174 ,且当前AGV目标点位是其他AGV所在点位 则让其他AGV先申请当前资源
                if (posList.contains(otherPos) && posList.contains(currentTargetPos)) {
                    return false;
                }
                posList = List.of("LM188", "LM187", "LM186", "LM185", "LM184");
                //其他AGV在LM188,LM187,LM186,LM185,LM184 ,且当前AGV目标点位是其他AGV所在点位 则让其他AGV先申请当前资源
                if (posList.contains(otherPos) && posList.contains(currentTargetPos)) {
                    return false;
                }
            }
        }

        if (resourceId.intValue() == 94) {//LM32,LM113,LM112,LM111
            String currentPos = this.getCurrentAgvPos(clientId);
            String otherPos = this.getOtherAgvPos(clientId);
            String currentTargetPos = currentAgvDto.getTargetPos().getCode();
            String otherTargetPos = this.getOtherTargetPos(otherAgvDto.getTargetPos());
            if ("LM31".equals(currentPos)) {
                List<String> posList = List.of("LM118", "LM117", "LM116", "LM115", "LM114", "LM113", "LM112", "LM111");
                //其他AGV在LM37 ,且其他AGV目标点位是LM118,LM117,LM116,LM115,LM114,LM113,LM112,LM111 则让其他AGV先申请当前资源
                if ("LM37".equals(otherPos) && posList.contains(otherTargetPos)) {
                    return false;
                }
            }
            if ("LM37".equals(currentPos)) {
                List<String> posList = List.of("LM118", "LM117", "LM116", "LM115", "LM114", "LM113", "LM112", "LM111");
                //其他AGV在LM31 ,且其他AGV目标点位是LM118,LM117,LM116,LM115,LM114,LM113,LM112,LM111 则让其他AGV先申请当前资源
                if ("LM31".equals(otherPos) && posList.contains(otherTargetPos)) {
                    return false;
                }
            }
            if (List.of("LM31", "LM37").contains(currentPos)) {
                //其他AGV在LM118,LM117,LM116,LM115,LM114, 且当前AGV目标点位是LM118,LM117,LM116,LM115,LM114
                //则让其他AGV先申请当前资源
                List<String> posList = List.of("LM118", "LM117", "LM116", "LM115", "LM114");
                if (posList.contains(otherPos) && posList.contains(currentTargetPos)) {
                    return false;
                }
            }
        }
        if (resourceId.intValue() == 95) {//LM37,LM123,LM122,LM121
            String currentPos = this.getCurrentAgvPos(clientId);
            String otherPos = this.getOtherAgvPos(clientId);
            String currentTargetPos = currentAgvDto.getTargetPos().getCode();
            String otherTargetPos = this.getOtherTargetPos(otherAgvDto.getTargetPos());
            if ("LM32".equals(currentPos)) {
                List<String> posList = List.of("LM128", "LM127", "LM126", "LM125", "LM124", "LM123", "LM122", "LM121");
                //其他AGV在LM48 ,且其他AGV目标点位是LM128,LM127,LM126,LM125,LM124,LM123,LM122,LM121, 则让其他AGV先申请当前资源
                if ("LM48".equals(otherPos) && posList.contains(otherTargetPos)) {
                    return false;
                }
            }
            if ("LM48".equals(currentPos)) {
                List<String> posList = List.of("LM128", "LM127", "LM126", "LM125", "LM124", "LM123", "LM122", "LM121");
                //其他AGV在LM32 ,且其他AGV目标点位是LM128,LM127,LM126,LM125,LM124,LM123,LM122,LM121, 则让其他AGV先申请当前资源
                if ("LM32".equals(otherPos) && posList.contains(otherTargetPos)) {
                    return false;
                }
            }
            if (List.of("LM32", "LM48").contains(currentPos)) {
                //其他AGV在LM128,LM127,LM126,LM125,LM124, 且当前AGV目标点位是LM128,LM127,LM126,LM125,LM124,
                //则让其他AGV先申请当前资源
                List<String> posList = List.of("LM128", "LM127", "LM126", "LM125", "LM124");
                if (posList.contains(otherPos) && posList.contains(currentTargetPos)) {
                    return false;
                }
            }
        }
        if (resourceId.intValue() == 96) {//LM48,LM133,LM132,LM131
            String currentPos = this.getCurrentAgvPos(clientId);
            String otherPos = this.getOtherAgvPos(clientId);
            String currentTargetPos = currentAgvDto.getTargetPos().getCode();
            String otherTargetPos = this.getOtherTargetPos(otherAgvDto.getTargetPos());
            if ("LM37".equals(currentPos)) {
                List<String> posList = List.of("LM138", "LM137", "LM136", "LM135", "LM134", "LM133", "LM132", "LM131");
                //其他AGV在LM49 ,且其他AGV目标点位是LM138,LM137,LM136,LM135,LM134,LM133,LM132,LM131, 则让其他AGV先申请当前资源
                if ("LM49".equals(otherPos) && posList.contains(otherTargetPos)) {
                    return false;
                }
            }
            if ("LM49".equals(currentPos)) {
                List<String> posList = List.of("LM138", "LM137", "LM136", "LM135", "LM134", "LM133", "LM132", "LM131");
                //其他AGV在LM37 ,且其他AGV目标点位是LM138,LM137,LM136,LM135,LM134,LM133,LM132,LM131, 则让其他AGV先申请当前资源
                if ("LM37".equals(otherPos) && posList.contains(otherTargetPos)) {
                    return false;
                }
            }
            if (List.of("LM37", "LM49").contains(currentPos)) {
                //其他AGV在LM138,LM137,LM136,LM135,LM134, 且当前AGV目标点位是LM138,LM137,LM136,LM135,LM134,
                //则让其他AGV先申请当前资源
                List<String> posList = List.of("LM138", "LM137", "LM136", "LM135", "LM134");
                if (posList.contains(otherPos) && posList.contains(currentTargetPos)) {
                    return false;
                }
            }
        }
        if (resourceId.intValue() == 97) {//LM49,LM143,LM142,LM141
            String currentPos = this.getCurrentAgvPos(clientId);
            String otherPos = this.getOtherAgvPos(clientId);
            String currentTargetPos = currentAgvDto.getTargetPos().getCode();
            String otherTargetPos = this.getOtherTargetPos(otherAgvDto.getTargetPos());
            if ("LM48".equals(currentPos)) {
                List<String> posList = List.of("LM148", "LM147", "LM146", "LM145", "LM144", "LM143", "LM142", "LM141");
                //其他AGV在LM50 ,且其他AGV目标点位是LM148,LM147,LM146,LM145,LM144,LM143,LM142,LM141, 则让其他AGV先申请当前资源
                if ("LM50".equals(otherPos) && posList.contains(otherTargetPos)) {
                    return false;
                }
            }
            if ("LM50".equals(currentPos)) {
                List<String> posList = List.of("LM148", "LM147", "LM146", "LM145", "LM144", "LM143", "LM142", "LM141");
                //其他AGV在LM48 ,且其他AGV目标点位是LM148,LM147,LM146,LM145,LM144,LM143,LM142,LM141, 则让其他AGV先申请当前资源
                if ("LM48".equals(otherPos) && posList.contains(otherTargetPos)) {
                    return false;
                }
            }
            if (List.of("LM48", "LM50").contains(currentPos)) {
                //其他AGV在LM148,LM147,LM146,LM145,LM144, 且当前AGV目标点位是LM148,LM147,LM146,LM145,LM144,
                //则让其他AGV先申请当前资源
                List<String> posList = List.of("LM148", "LM147", "LM146", "LM145", "LM144");
                if (posList.contains(otherPos) && posList.contains(currentTargetPos)) {
                    return false;
                }
            }
        }
        if (resourceId.intValue() == 98) {//LM50,LM153,LM152,LM151
            String currentPos = this.getCurrentAgvPos(clientId);
            String otherPos = this.getOtherAgvPos(clientId);
            String currentTargetPos = currentAgvDto.getTargetPos().getCode();
            String otherTargetPos = this.getOtherTargetPos(otherAgvDto.getTargetPos());
            if ("LM49".equals(currentPos)) {
                List<String> posList = List.of("LM158", "LM157", "LM156", "LM155", "LM154", "LM153", "LM152", "LM151");
                //其他AGV在LM51 ,且其他AGV目标点位是LM158,LM157,LM156,LM155,LM154,LM153,LM152,LM151, 则让其他AGV先申请当前资源
                if ("LM51".equals(otherPos) && posList.contains(otherTargetPos)) {
                    return false;
                }
            }
            if ("LM51".equals(currentPos)) {
                List<String> posList = List.of("LM158", "LM157", "LM156", "LM155", "LM154", "LM153", "LM152", "LM151");
                //其他AGV在LM49 ,且其他AGV目标点位是LM158,LM157,LM156,LM155,LM154,LM153,LM152,LM151, 则让其他AGV先申请当前资源
                if ("LM49".equals(otherPos) && posList.contains(otherTargetPos)) {
                    return false;
                }
            }

            if (List.of("LM49", "LM51").contains(currentPos)) {
                //其他AGV在LM158,LM157,LM156,LM155,LM154, 且当前AGV目标点位是LM158,LM157,LM156,LM155,LM154,
                //则让其他AGV先申请当前资源
                List<String> posList = List.of("LM158", "LM157", "LM156", "LM155", "LM154");
                if (posList.contains(otherPos) && posList.contains(currentTargetPos)) {
                    return false;
                }
            }
        }
        if (resourceId.intValue() == 99) {//LM51,LM163,LM162,LM161
            String currentPos = this.getCurrentAgvPos(clientId);
            String otherPos = this.getOtherAgvPos(clientId);
            String currentTargetPos = currentAgvDto.getTargetPos().getCode();
            String otherTargetPos = this.getOtherTargetPos(otherAgvDto.getTargetPos());
            if ("LM50".equals(currentPos)) {
                List<String> posList = List.of("LM168", "LM167", "LM166", "LM165", "LM164", "LM163", "LM162", "LM161");
                //其他AGV在LM52 ,且其他AGV目标点位是LM168,LM167,LM166,LM165,LM164,LM163,LM162,LM161, 则让其他AGV先申请当前资源
                if ("LM52".equals(otherPos) && posList.contains(otherTargetPos)) {
                    return false;
                }
            }
            if ("LM52".equals(currentPos)) {
                List<String> posList = List.of("LM168", "LM167", "LM166", "LM165", "LM164", "LM163", "LM162", "LM161");
                //其他AGV在LM50 ,且其他AGV目标点位是LM168,LM167,LM166,LM165,LM164,LM163,LM162,LM161, 则让其他AGV先申请当前资源
                if ("LM50".equals(otherPos) && posList.contains(otherTargetPos)) {
                    return false;
                }
            }
            if (List.of("LM50", "LM52").contains(currentPos)) {
                //其他AGV在LM168,LM167,LM166,LM165,LM164, 且当前AGV目标点位是LM168,LM167,LM166,LM165,LM164,
                //则让其他AGV先申请当前资源
                List<String> posList = List.of("LM168", "LM167", "LM166", "LM165", "LM164");
                if (posList.contains(otherPos) && posList.contains(currentTargetPos)) {
                    return false;
                }
            }
        }
        if (resourceId.intValue() == 100) {//LM52,LM173,LM172,LM171
            String currentPos = this.getCurrentAgvPos(clientId);
            String otherPos = this.getOtherAgvPos(clientId);
            String currentTargetPos = currentAgvDto.getTargetPos().getCode();
            String otherTargetPos = this.getOtherTargetPos(otherAgvDto.getTargetPos());
            if ("LM51".equals(currentPos)) {
                List<String> posList = List.of("LM178", "LM177", "LM176", "LM175", "LM174", "LM173", "LM172", "LM171");
                //其他AGV在LM33 ,且其他AGV目标点位是LM178,LM177,LM176,LM175,LM174,LM173,LM172,LM171, 则让其他AGV先申请当前资源
                if ("LM33".equals(otherPos) && posList.contains(otherTargetPos)) {
                    return false;
                }
            }
            if ("LM33".equals(currentPos)) {
                List<String> posList = List.of("LM178", "LM177", "LM176", "LM175", "LM174", "LM173", "LM172", "LM171");
                //其他AGV在LM51 ,且其他AGV目标点位是LM178,LM177,LM176,LM175,LM174,LM173,LM172,LM171, 则让其他AGV先申请当前资源
                if ("LM51".equals(otherPos) && posList.contains(otherTargetPos)) {
                    return false;
                }
            }
            if (List.of("LM51", "LM33").contains(currentPos)) {
                //其他AGV在LM178,LM177,LM176,LM175,LM174, 且当前AGV目标点位是LM178,LM177,LM176,LM175,LM174,
                //则让其他AGV先申请当前资源
                List<String> posList = List.of("LM178", "LM177", "LM176", "LM175", "LM174");
                if (posList.contains(otherPos) && posList.contains(currentTargetPos)) {
                    return false;
                }
            }
        }
        if (resourceId.intValue() == 101) {//LM33,LM183,LM182,LM181
            String currentPos = this.getCurrentAgvPos(clientId);
            String otherPos = this.getOtherAgvPos(clientId);
            String currentTargetPos = currentAgvDto.getTargetPos().getCode();
            String otherTargetPos = this.getOtherTargetPos(otherAgvDto.getTargetPos());
            if ("LM52".equals(currentPos)) {
                List<String> posList = List.of("LM188", "LM187", "LM186", "LM185", "LM184", "LM183", "LM182", "LM181");
                //其他AGV在LM190 ,且其他AGV目标点位是LM188,LM187,LM186,LM185,LM184,LM183,LM182,LM181 则让其他AGV先申请当前资源
                if ("LM190".equals(otherPos) && posList.contains(otherTargetPos)) {
                    return false;
                }
            }
            if ("LM190".equals(currentPos)) {
                List<String> posList = List.of("LM188", "LM187", "LM186", "LM185", "LM184", "LM183", "LM182", "LM181");
                //其他AGV在LM52 ,且其他AGV目标点位是LM188,LM187,LM186,LM185,LM184,LM183,LM182,LM181 则让其他AGV先申请当前资源
                if ("LM52".equals(otherPos) && posList.contains(otherTargetPos)) {
                    return false;
                }
            }
            if (List.of("LM52", "LM190").contains(currentPos)) {
                //其他AGV在LM188,LM187,LM186,LM185,LM184, 且当前AGV目标点位是LM188,LM187,LM186,LM185,LM184,
                //则让其他AGV先申请当前资源
                List<String> posList = List.of("LM188", "LM187", "LM186", "LM185", "LM184");
                if (posList.contains(otherPos) && posList.contains(currentTargetPos)) {
                    return false;
                }
            }
        }

        if (resourceId.intValue() == 194) {//LM196,LM195,LM22
            String currentPos = this.getCurrentAgvPos(clientId);
            String otherPos = this.getOtherAgvPos(clientId);
            String currentTargetPos = currentAgvDto.getTargetPos().getCode();
            String otherTargetPos = this.getOtherTargetPos(otherAgvDto.getTargetPos());
            if (List.of("LM1", "LM24", "LM23").contains(currentPos)) {
                List<String> posList = List.of("LM194", "LM2");
                //其他AGV在LM194,LM2 ,且当前AGV目标点位是LM22 则让其他AGV先申请当前资源
                if (posList.contains(otherPos) && "LM22".equals(currentTargetPos)) {
                    return false;
                }
            }
        }
        if (resourceId.intValue() == 214) {//LM199,LM198,LM23
            String currentPos = this.getCurrentAgvPos(clientId);
            String otherPos = this.getOtherAgvPos(clientId);
            String currentTargetPos = currentAgvDto.getTargetPos().getCode();
            String otherTargetPos = this.getOtherTargetPos(otherAgvDto.getTargetPos());
            if (List.of("LM22", "LM25").contains(currentPos)) {
                List<String> posList = List.of("LM197", "LM3");
                //其他AGV在LM197,LM3 ,且当前AGV目标点位是LM23 则让其他AGV先申请当前资源
                if (posList.contains(otherPos) && "LM23".equals(currentTargetPos)) {
                    return false;
                }
            }
        }
        if (resourceId.intValue() == 244) {//LM31
            String currentPos = this.getCurrentAgvPos(clientId);
            String otherPos = this.getOtherAgvPos(clientId);
            String currentTargetPos = currentAgvDto.getTargetPos().getCode();
            String otherTargetPos = this.getOtherTargetPos(otherAgvDto.getTargetPos());
            if (List.of("LM32", "LM191").contains(currentPos)) {
                List<String> posList = List.of("LM5", "LM200", "LM201", "LM202",
                        "LM210", "LM236", "LM29", "LM235", "LM212", "LM28", "LM54");
                //其他AGV在LM5,LM200,LM201,LM202,LM210,LM236,LM29,LM235,LM212,LM28,LM54 ,且其他AGV目标点位不是LM57
                // 且当前AGV目标点位是LM29 则让其他AGV先申请当前资源
                if (posList.contains(otherPos) && !"LM57".equals(otherTargetPos) && "LM29".equals(currentTargetPos)) {
                    return false;
                }
            }
            if (List.of("LM191").contains(currentPos)) {
                List<String> posList = List.of("LM203", "LM207", "LM208", "LM209", "LM218", "LM57", "LM242", "LM243", "LM244", "LM245", "LM246", "LM61", "LM60", "LM215", "LM216", "LM58", "LM255",
                        "LM236", "LM29", "LM235", "LM212", "LM28", "LM54");
                //其他AGV在LM203,LM207,LM208,LM209,LM218,LM57,LM242,LM243,LM244,LM245,LM246,LM61,LM60,LM215,LM216,LM58,LM255
                // LM236,LM29,LM235,LM212,LM28,LM54,且当前AGV目标点位是LM57 则让其他AGV先申请当前资源
                if (posList.contains(otherPos) && "LM57".equals(currentTargetPos)) {
                    return false;
                }
            }
            if ("LM54".equals(currentPos)) {
                //其他AGV在LM191,LM4,LM240,LM26 ,且其他AGV目标点位不是LM57和LM29
                //且当前AGV目标点位是LM1,LM34,LM22,LM23,LM25 则让其他AGV先申请当前资源
                List<String> posList = List.of("LM191", "LM4", "LM240", "LM26");
                if (posList.contains(otherPos) && !List.of("LM57", "LM29").contains(otherTargetPos)
                        && List.of("LM1", "LM34", "LM22", "LM23", "LM25").contains(currentTargetPos)) {
                    return false;
                }
            }
        }

        return true;
    }

    /**
     * 处理AGV阻塞（前往已有点位进行避让）
     */
    @Scheduled(fixedDelay = 1000)
    private void findAgvBlockScheduled() {
        try {
            //出现两车彼此 申请对方当前所占资源
            if (this.allAgvBlockForApplyResource()) {
                Long srAgv1ApplyId = blockForApplyResource.get("sr_agv1");
                Long srAgv2ApplyId = blockForApplyResource.get("sr_agv2");
                String srAgv1CurrentPos = this.getCurrentAgvPos("sr_agv1");
                String srAgv2CurrentPos = this.getCurrentAgvPos("sr_agv2");
                String srAgv1TargetPos = this.getCurrentAgvDto("sr_agv1").getTargetPos().getCode();
                String srAgv2TargetPos = this.getCurrentAgvDto("sr_agv2").getTargetPos().getCode();
                if (srAgv1ApplyId != null && srAgv2ApplyId != null) {
                    PathResourceEntity srAgv1ApplyResource = pathResourceMapper.selectById(srAgv1ApplyId);
                    PathResourceEntity srAgv2ApplyResource = pathResourceMapper.selectById(srAgv2ApplyId);
                    log.warn("\n[sr_agv1]申请资源冲突 [sr_agv2]申请资源冲突==>\n [sr_agv1]当前位置：{} [sr_agv1]申请的资源：{}\n [sr_agv2]当前位置：{} [sr_agv2]申请的资源：{}"
                            , srAgv1CurrentPos, srAgv1ApplyResource.getPathList(), srAgv2CurrentPos, srAgv2ApplyResource.getPathList());
                    List<HandleAgvBlockDto> list = new ArrayList<>();
                    list.add(new HandleAgvBlockDto("sr_agv1", srAgv1CurrentPos, srAgv1TargetPos, srAgv1ApplyId));
                    list.add(new HandleAgvBlockDto("sr_agv2", srAgv2CurrentPos, srAgv2TargetPos, srAgv2ApplyId));
                    this.handleAgvBlock(list);
                }
            }
        } catch (Exception e) {
            log.error("处理AGV阻塞异常", e.getMessage());
        }
    }

    /**
     * 处理AGV阻塞
     *
     * @param list
     */
    private void handleAgvBlock(List<HandleAgvBlockDto> list) {
        List<Long> resourceIdList = list.stream().map(HandleAgvBlockDto::getResourceId).collect(Collectors.toList());
        //资源194 (LM196,LM195,LM22) 和 资源196 (LM24) 存在AGV相互申请
        if (resourceIdList.contains(194L) && resourceIdList.contains(196L)) {
            this.handleAgvBlock194With196(list);
        }
        //资源214 (LM199,LM198,LM23) 和 资源216 (LM25) 存在AGV相互申请
        if (resourceIdList.contains(214L) && resourceIdList.contains(216L)) {
            this.handleAgvBlock214With216(list);
        }
        //资源194 (LM196,LM195,LM22) 和 资源214 (LM199,LM198,LM23) 存在AGV相互申请
        if (resourceIdList.contains(194L) && resourceIdList.contains(214L)) {
            this.handleAgvBlock194With214(list);
        }
        //资源196 (LM24) 和 资源216 (LM25) 存在AGV相互申请
        if (resourceIdList.contains(196L) && resourceIdList.contains(216L)) {
            this.handleAgvBlock196With216(list);
        }
        //资源196 (LM24) 和 资源234 (LM191,LM4,LM240,LM26) 存在AGV相互申请
        if (resourceIdList.contains(196L) && resourceIdList.contains(234L)) {
            this.handleAgvBlock196With234(list);
        }
        //资源244 (LM31) 和 资源234 (LM191,LM4,LM240,LM26) 存在AGV相互申请
        if (resourceIdList.contains(244L) && resourceIdList.contains(234L)) {
            this.handleAgvBlock244With234(list);
        }
        //资源244 (LM31) 和 资源94 (LM32,LM113,LM112,LM111) 存在AGV相互申请
        if (resourceIdList.contains(244L) && resourceIdList.contains(94L)) {
            this.handleAgvBlock244With94(list);
        }
        //资源244 (LM31) 和 资源84 (LM202,LM210,LM236,LM29,LM235,LM212,LM28,LM54) 存在AGV相互申请
        if (resourceIdList.contains(244L) && resourceIdList.contains(84L)) {
            this.handleAgvBlock244With84(list);
        }
        //资源84 (LM202,LM210,LM236,LM29,LM235,LM212,LM28,LM54) 和 资源24 (LM203,LM207,LM208,LM209,LM..) 存在AGV相互申请
        if (resourceIdList.contains(84L) && resourceIdList.contains(24L)) {
            this.handleAgvBlock84With24(list);
        }
        // 资源101 (LM33,LM183,LM182,LM181) 和 资源66 (LM249,LM234,LM233,LM222,LM190) 存在AGV相互申请
        if (resourceIdList.contains(101L) && resourceIdList.contains(66L)) {
            this.handleAgvBlock101With66(list);
        }

        // 资源101 (LM33,LM183,LM182,LM181) 和 资源100 (LM52,LM173,LM172,LM171) 存在AGV相互申请
        if (resourceIdList.contains(101L) && resourceIdList.contains(100L)) {
            this.handleAgvBlock101With100(list);
        }
        // 资源100 (LM52,LM173,LM172,LM171) 和 资源99 (LM51,LM163,LM162,LM161) 存在AGV相互申请
        if (resourceIdList.contains(100L) && resourceIdList.contains(99L)) {
            this.handleAgvBlock100With99(list);
        }
        // 资源99 (LM51,LM163,LM162,LM161) 和 资源98 (LM50,LM153,LM152,LM151) 存在AGV相互申请
        if (resourceIdList.contains(99L) && resourceIdList.contains(98L)) {
            this.handleAgvBlock99With98(list);
        }
        // 资源98 (LM50,LM153,LM152,LM151) 和 资源97 (LM49,LM143,LM142,LM141) 存在AGV相互申请
        if (resourceIdList.contains(98L) && resourceIdList.contains(97L)) {
            this.handleAgvBlock98With97(list);
        }
        // 资源97 (LM49,LM143,LM142,LM141) 和 资源96 (LM48,LM133,LM132,LM131) 存在AGV相互申请
        if (resourceIdList.contains(97L) && resourceIdList.contains(96L)) {
            this.handleAgvBlock97With96(list);
        }
        // 资源96 (LM48,LM133,LM132,LM131) 和 资源95 (LM37,LM123,LM122,LM121) 存在AGV相互申请
        if (resourceIdList.contains(96L) && resourceIdList.contains(95L)) {
            this.handleAgvBlock96With95(list);
        }
        // 资源95 (LM37,LM123,LM122,LM121) 和 资源94 (LM32,LM113,LM112,LM111) 存在AGV相互申请
        if (resourceIdList.contains(95L) && resourceIdList.contains(94L)) {
            this.handleAgvBlock95With94(list);
        }
        //资源64 (LM231,LM232,LM248,LM249,LM234,LM233,LM222,LM190) 和 资源68 (LM211,LM55) 存在AGV相互申请
        if (resourceIdList.contains(64L) && resourceIdList.contains(68L)) {
            this.handleAgvBlock64With68(list);
        }
        //资源68 (LM211,LM55) 和 资源44 (LM217,LM219,LM247,LM220,LM230) 存在AGV相互申请
        if (resourceIdList.contains(68L) && resourceIdList.contains(44L)) {
            this.handleAgvBlock68With44(list);
        }
    }

    // 资源95 (LM37,LM123,LM122,LM121) 和 资源94 (LM32,LM113,LM112,LM111) 存在AGV相互申请
    private void handleAgvBlock95With94(List<HandleAgvBlockDto> list) {
        Map<String, HandleAgvBlockDto> map = list.stream()
                .collect(Collectors.toMap(HandleAgvBlockDto::getCurrentPos, v -> v));
        HandleAgvBlockDto agv1 = map.get("LM37");
        HandleAgvBlockDto agv2 = map.get("LM32");
        Cache<String, RobotNavInfoDto> robotNavInfoDtoMapCache = SpringUtil.getBean("robotNavInfo");
        RobotNavInfoDto agvNavDto1 = robotNavInfoDtoMapCache.getIfPresent(agv1.getRobotSn());
        RobotNavInfoDto agvNavDto2 = robotNavInfoDtoMapCache.getIfPresent(agv2.getRobotSn());
        String clientId = "";
        String avoidPos = "";
        String currentPos = "";
        String targetPos = "";
        if (!agvNavDto1.getIsFull()) {//agv1空车 则进缓存架避让
            clientId = agv1.getRobotSn();
            avoidPos = "LM128";
            currentPos = agv1.getCurrentPos();
            targetPos = agv1.getTargetPos();
        } else if (!agvNavDto2.getIsFull()) {//agv2空车 则进缓存架避让
            clientId = agv2.getRobotSn();
            avoidPos = "LM118";
            currentPos = agv2.getCurrentPos();
            targetPos = agv2.getTargetPos();
        }
        RobotApplyGotoTargetDto applyDto = new RobotApplyGotoTargetDto();
        applyDto.setClientId(clientId);
        applyDto.setCurrentPos(currentPos);
        applyDto.setTargetPos(targetPos);
        LinkedHashMap<String, CheckOperationFunction> middlePos = new LinkedHashMap<>();
        final String finalClientId = clientId;
        middlePos.put(avoidPos, () -> {
            try {
                //LM37,LM32,
                List<String> posList = List.of("LM37", "LM32");
                String otherPos = this.getOtherAgvPos(finalClientId);
                while (posList.contains(otherPos)) {
                    otherPos = this.getOtherAgvPos(finalClientId);
                    TimeUnit.SECONDS.sleep(1);
                }
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }
        });
        applyDto.setMiddlePos(middlePos);
        //重新下发路径资源申请
        SpringUtil.getApplicationContext().publishEvent(applyDto);
    }

    // 资源96 (LM48,LM133,LM132,LM131) 和 资源95 (LM37,LM123,LM122,LM121) 存在AGV相互申请
    private void handleAgvBlock96With95(List<HandleAgvBlockDto> list) {
        Map<String, HandleAgvBlockDto> map = list.stream()
                .collect(Collectors.toMap(HandleAgvBlockDto::getCurrentPos, v -> v));
        HandleAgvBlockDto agv1 = map.get("LM48");
        HandleAgvBlockDto agv2 = map.get("LM37");
        Cache<String, RobotNavInfoDto> robotNavInfoDtoMapCache = SpringUtil.getBean("robotNavInfo");
        RobotNavInfoDto agvNavDto1 = robotNavInfoDtoMapCache.getIfPresent(agv1.getRobotSn());
        RobotNavInfoDto agvNavDto2 = robotNavInfoDtoMapCache.getIfPresent(agv2.getRobotSn());
        String clientId = "";
        String avoidPos = "";
        String currentPos = "";
        String targetPos = "";
        if (!agvNavDto1.getIsFull()) {//agv1空车 则进缓存架避让
            clientId = agv1.getRobotSn();
            avoidPos = "LM138";
            currentPos = agv1.getCurrentPos();
            targetPos = agv1.getTargetPos();
        } else if (!agvNavDto2.getIsFull()) {//agv2空车 则进缓存架避让
            clientId = agv2.getRobotSn();
            avoidPos = "LM128";
            currentPos = agv2.getCurrentPos();
            targetPos = agv2.getTargetPos();
        }
        RobotApplyGotoTargetDto applyDto = new RobotApplyGotoTargetDto();
        applyDto.setClientId(clientId);
        applyDto.setCurrentPos(currentPos);
        applyDto.setTargetPos(targetPos);
        LinkedHashMap<String, CheckOperationFunction> middlePos = new LinkedHashMap<>();
        final String finalClientId = clientId;
        middlePos.put(avoidPos, () -> {
            try {
                //LM48,LM37,
                List<String> posList = List.of("LM48", "LM37");
                String otherPos = this.getOtherAgvPos(finalClientId);
                while (posList.contains(otherPos)) {
                    otherPos = this.getOtherAgvPos(finalClientId);
                    TimeUnit.SECONDS.sleep(1);
                }
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }
        });
        applyDto.setMiddlePos(middlePos);
        //重新下发路径资源申请
        SpringUtil.getApplicationContext().publishEvent(applyDto);
    }

    // 资源97 (LM49,LM143,LM142,LM141) 和 资源96 (LM48,LM133,LM132,LM131) 存在AGV相互申请
    private void handleAgvBlock97With96(List<HandleAgvBlockDto> list) {
        Map<String, HandleAgvBlockDto> map = list.stream()
                .collect(Collectors.toMap(HandleAgvBlockDto::getCurrentPos, v -> v));
        HandleAgvBlockDto agv1 = map.get("LM49");
        HandleAgvBlockDto agv2 = map.get("LM48");
        Cache<String, RobotNavInfoDto> robotNavInfoDtoMapCache = SpringUtil.getBean("robotNavInfo");
        RobotNavInfoDto agvNavDto1 = robotNavInfoDtoMapCache.getIfPresent(agv1.getRobotSn());
        RobotNavInfoDto agvNavDto2 = robotNavInfoDtoMapCache.getIfPresent(agv2.getRobotSn());
        String clientId = "";
        String avoidPos = "";
        String currentPos = "";
        String targetPos = "";
        if (!agvNavDto1.getIsFull()) {//agv1空车 则进缓存架避让
            clientId = agv1.getRobotSn();
            avoidPos = "LM148";
            currentPos = agv1.getCurrentPos();
            targetPos = agv1.getTargetPos();
        } else if (!agvNavDto2.getIsFull()) {//agv2空车 则进缓存架避让
            clientId = agv2.getRobotSn();
            avoidPos = "LM138";
            currentPos = agv2.getCurrentPos();
            targetPos = agv2.getTargetPos();
        }
        RobotApplyGotoTargetDto applyDto = new RobotApplyGotoTargetDto();
        applyDto.setClientId(clientId);
        applyDto.setCurrentPos(currentPos);
        applyDto.setTargetPos(targetPos);
        LinkedHashMap<String, CheckOperationFunction> middlePos = new LinkedHashMap<>();
        final String finalClientId = clientId;
        middlePos.put(avoidPos, () -> {
            try {
                //LM49,LM48,
                List<String> posList = List.of("LM49", "LM48");
                String otherPos = this.getOtherAgvPos(finalClientId);
                while (posList.contains(otherPos)) {
                    otherPos = this.getOtherAgvPos(finalClientId);
                    TimeUnit.SECONDS.sleep(1);
                }
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }
        });
        applyDto.setMiddlePos(middlePos);
        //重新下发路径资源申请
        SpringUtil.getApplicationContext().publishEvent(applyDto);
    }

    // 资源98 (LM50,LM153,LM152,LM151) 和 资源97 (LM49,LM143,LM142,LM141) 存在AGV相互申请
    private void handleAgvBlock98With97(List<HandleAgvBlockDto> list) {
        Map<String, HandleAgvBlockDto> map = list.stream()
                .collect(Collectors.toMap(HandleAgvBlockDto::getCurrentPos, v -> v));
        HandleAgvBlockDto agv1 = map.get("LM50");
        HandleAgvBlockDto agv2 = map.get("LM49");
        Cache<String, RobotNavInfoDto> robotNavInfoDtoMapCache = SpringUtil.getBean("robotNavInfo");
        RobotNavInfoDto agvNavDto1 = robotNavInfoDtoMapCache.getIfPresent(agv1.getRobotSn());
        RobotNavInfoDto agvNavDto2 = robotNavInfoDtoMapCache.getIfPresent(agv2.getRobotSn());
        String clientId = "";
        String avoidPos = "";
        String currentPos = "";
        String targetPos = "";
        if (!agvNavDto1.getIsFull()) {//agv1空车 则进缓存架避让
            clientId = agv1.getRobotSn();
            avoidPos = "LM158";
            currentPos = agv1.getCurrentPos();
            targetPos = agv1.getTargetPos();
        } else if (!agvNavDto2.getIsFull()) {//agv2空车 则进缓存架避让
            clientId = agv2.getRobotSn();
            avoidPos = "LM148";
            currentPos = agv2.getCurrentPos();
            targetPos = agv2.getTargetPos();
        }
        RobotApplyGotoTargetDto applyDto = new RobotApplyGotoTargetDto();
        applyDto.setClientId(clientId);
        applyDto.setCurrentPos(currentPos);
        applyDto.setTargetPos(targetPos);
        LinkedHashMap<String, CheckOperationFunction> middlePos = new LinkedHashMap<>();
        final String finalClientId = clientId;
        middlePos.put(avoidPos, () -> {
            try {
                //LM50,LM49,
                List<String> posList = List.of("LM50", "LM49");
                String otherPos = this.getOtherAgvPos(finalClientId);
                while (posList.contains(otherPos)) {
                    otherPos = this.getOtherAgvPos(finalClientId);
                    TimeUnit.SECONDS.sleep(1);
                }
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }
        });
        applyDto.setMiddlePos(middlePos);
        //重新下发路径资源申请
        SpringUtil.getApplicationContext().publishEvent(applyDto);
    }

    // 资源99 (LM51,LM163,LM162,LM161) 和 资源98 (LM50,LM153,LM152,LM151) 存在AGV相互申请
    private void handleAgvBlock99With98(List<HandleAgvBlockDto> list) {
        Map<String, HandleAgvBlockDto> map = list.stream()
                .collect(Collectors.toMap(HandleAgvBlockDto::getCurrentPos, v -> v));
        HandleAgvBlockDto agv1 = map.get("LM51");
        HandleAgvBlockDto agv2 = map.get("LM50");
        Cache<String, RobotNavInfoDto> robotNavInfoDtoMapCache = SpringUtil.getBean("robotNavInfo");
        RobotNavInfoDto agvNavDto1 = robotNavInfoDtoMapCache.getIfPresent(agv1.getRobotSn());
        RobotNavInfoDto agvNavDto2 = robotNavInfoDtoMapCache.getIfPresent(agv2.getRobotSn());
        String clientId = "";
        String avoidPos = "";
        String currentPos = "";
        String targetPos = "";
        if (!agvNavDto1.getIsFull()) {//agv1空车 则进缓存架避让
            clientId = agv1.getRobotSn();
            avoidPos = "LM168";
            currentPos = agv1.getCurrentPos();
            targetPos = agv1.getTargetPos();
        } else if (!agvNavDto2.getIsFull()) {//agv2空车 则进缓存架避让
            clientId = agv2.getRobotSn();
            avoidPos = "LM158";
            currentPos = agv2.getCurrentPos();
            targetPos = agv2.getTargetPos();
        }
        RobotApplyGotoTargetDto applyDto = new RobotApplyGotoTargetDto();
        applyDto.setClientId(clientId);
        applyDto.setCurrentPos(currentPos);
        applyDto.setTargetPos(targetPos);
        LinkedHashMap<String, CheckOperationFunction> middlePos = new LinkedHashMap<>();
        final String finalClientId = clientId;
        middlePos.put(avoidPos, () -> {
            try {
                //LM51,LM50,
                List<String> posList = List.of("LM51", "LM50");
                String otherPos = this.getOtherAgvPos(finalClientId);
                while (posList.contains(otherPos)) {
                    otherPos = this.getOtherAgvPos(finalClientId);
                    TimeUnit.SECONDS.sleep(1);
                }
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }
        });
        applyDto.setMiddlePos(middlePos);
        //重新下发路径资源申请
        SpringUtil.getApplicationContext().publishEvent(applyDto);
    }

    // 资源100 (LM52,LM173,LM172,LM171) 和 资源99 (LM51,LM163,LM162,LM161) 存在AGV相互申请
    private void handleAgvBlock100With99(List<HandleAgvBlockDto> list) {
        Map<String, HandleAgvBlockDto> map = list.stream()
                .collect(Collectors.toMap(HandleAgvBlockDto::getCurrentPos, v -> v));
        HandleAgvBlockDto agv1 = map.get("LM52");
        HandleAgvBlockDto agv2 = map.get("LM51");
        Cache<String, RobotNavInfoDto> robotNavInfoDtoMapCache = SpringUtil.getBean("robotNavInfo");
        RobotNavInfoDto agvNavDto1 = robotNavInfoDtoMapCache.getIfPresent(agv1.getRobotSn());
        RobotNavInfoDto agvNavDto2 = robotNavInfoDtoMapCache.getIfPresent(agv2.getRobotSn());
        String clientId = "";
        String avoidPos = "";
        String currentPos = "";
        String targetPos = "";
        if (!agvNavDto1.getIsFull()) {//agv1空车 则进缓存架避让
            clientId = agv1.getRobotSn();
            avoidPos = "LM178";
            currentPos = agv1.getCurrentPos();
            targetPos = agv1.getTargetPos();
        } else if (!agvNavDto2.getIsFull()) {//agv2空车 则进缓存架避让
            clientId = agv2.getRobotSn();
            avoidPos = "LM168";
            currentPos = agv2.getCurrentPos();
            targetPos = agv2.getTargetPos();
        }
        RobotApplyGotoTargetDto applyDto = new RobotApplyGotoTargetDto();
        applyDto.setClientId(clientId);
        applyDto.setCurrentPos(currentPos);
        applyDto.setTargetPos(targetPos);
        LinkedHashMap<String, CheckOperationFunction> middlePos = new LinkedHashMap<>();
        final String finalClientId = clientId;
        middlePos.put(avoidPos, () -> {
            try {
                //LM52,LM51,
                List<String> posList = List.of("LM52", "LM51");
                String otherPos = this.getOtherAgvPos(finalClientId);
                while (posList.contains(otherPos)) {
                    otherPos = this.getOtherAgvPos(finalClientId);
                    TimeUnit.SECONDS.sleep(1);
                }
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }
        });
        applyDto.setMiddlePos(middlePos);
        //重新下发路径资源申请
        SpringUtil.getApplicationContext().publishEvent(applyDto);
    }

    // 资源101 (LM33,LM183,LM182,LM181) 和 资源100 (LM52,LM173,LM172,LM171) 存在AGV相互申请
    private void handleAgvBlock101With100(List<HandleAgvBlockDto> list) {
        Map<String, HandleAgvBlockDto> map = list.stream()
                .collect(Collectors.toMap(HandleAgvBlockDto::getCurrentPos, v -> v));
        HandleAgvBlockDto agv1 = map.get("LM33");
        HandleAgvBlockDto agv2 = map.get("LM52");
        Cache<String, RobotNavInfoDto> robotNavInfoDtoMapCache = SpringUtil.getBean("robotNavInfo");
        RobotNavInfoDto agvNavDto1 = robotNavInfoDtoMapCache.getIfPresent(agv1.getRobotSn());
        RobotNavInfoDto agvNavDto2 = robotNavInfoDtoMapCache.getIfPresent(agv2.getRobotSn());
        String agv1TargetPos = agvNavDto1.getTargetPos().getCode();
        String agv2TargetPos = agvNavDto2.getTargetPos().getCode();
        String clientId = "";
        String avoidPos = "";
        String currentPos = "";
        String targetPos = "";
        List<String> waitPosList = new ArrayList<>();

        if (!agvNavDto1.getIsFull()) {//agv1空车
            //LM188,LM187,LM186,LM185,LM184,LM183,LM182,LM181
            List<String> posList = List.of("LM188", "LM187","LM186","LM185", "LM184", "LM183", "LM182", "LM181");
            clientId = agv1.getRobotSn();
            currentPos = agv1.getCurrentPos();
            targetPos = agv1.getTargetPos();
            waitPosList = List.of("LM33", "LM52");
            if(!posList.contains(agv2TargetPos)){//进缓存架避让
                avoidPos = "LM188";
            }else {//后退一个点位进行避让
                avoidPos = "LM190";
            }

        }
        if (!agvNavDto2.getIsFull()) {//agv2空车 则进缓存架避让
            clientId = agv2.getRobotSn();
            avoidPos = "LM178";
            currentPos = agv2.getCurrentPos();
            targetPos = agv2.getTargetPos();
        }
        RobotApplyGotoTargetDto applyDto = new RobotApplyGotoTargetDto();
        applyDto.setClientId(clientId);
        applyDto.setCurrentPos(currentPos);
        applyDto.setTargetPos(targetPos);
        LinkedHashMap<String, CheckOperationFunction> middlePos = new LinkedHashMap<>();
        final String finalClientId = clientId;
        final List<String> finalWaitPosList = waitPosList;
        middlePos.put(avoidPos, () -> {
            try {
                //LM33,LM52,
                List<String> posList = List.of("LM33", "LM52");
                String otherPos = this.getOtherAgvPos(finalClientId);
                while (posList.contains(otherPos)) {
                    otherPos = this.getOtherAgvPos(finalClientId);
                    TimeUnit.SECONDS.sleep(1);
                }
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }
        });
        applyDto.setMiddlePos(middlePos);
        //重新下发路径资源申请
        SpringUtil.getApplicationContext().publishEvent(applyDto);
    }


    //资源68 (LM211,LM55) 和 资源44 (LM217,LM219,LM247,LM220,LM230) 存在AGV相互申请
    private void handleAgvBlock68With44(List<HandleAgvBlockDto> list) {
        Map<String, HandleAgvBlockDto> map = list.stream()
                .collect(Collectors.toMap(HandleAgvBlockDto::getCurrentPos, v -> v));
        HandleAgvBlockDto avoidAgv = map.get("LM55");
        if (avoidAgv != null) {//AGV前往LM250避让，直到其他AGV离开资源68和44
            String clientId = avoidAgv.getRobotSn();
            String avoidPos = "LM250";
            RobotApplyGotoTargetDto applyDto = new RobotApplyGotoTargetDto();
            applyDto.setClientId(clientId);
            applyDto.setCurrentPos(avoidAgv.getCurrentPos());
            applyDto.setTargetPos(avoidAgv.getTargetPos());
            LinkedHashMap<String, CheckOperationFunction> middlePos = new LinkedHashMap<>();
            middlePos.put(avoidPos, () -> {
                try {
                    List<String> posList = List.of("LM230", "LM55");
                    String otherPos = this.getOtherAgvPos(clientId);
                    while (posList.contains(otherPos)) {
                        otherPos = this.getOtherAgvPos(clientId);
                        TimeUnit.SECONDS.sleep(1);
                    }
                } catch (Exception e) {
                    log.error(e.getMessage(), e);
                }
            });
            applyDto.setMiddlePos(middlePos);
            //重新下发路径资源申请
            SpringUtil.getApplicationContext().publishEvent(applyDto);
        }
    }


    //资源64 (LM231,LM232,LM248,LM249,LM234,LM233,LM222,LM190) 和 资源68 (LM211,LM55) 存在AGV相互申请
    private void handleAgvBlock64With68(List<HandleAgvBlockDto> list) {
        Map<String, HandleAgvBlockDto> map = list.stream()
                .collect(Collectors.toMap(HandleAgvBlockDto::getCurrentPos, v -> v));
        HandleAgvBlockDto avoidAgv = map.get("LM55");
        if (avoidAgv != null) {//AGV前往LM250避让，直到其他AGV离开资源64和68
            String clientId = avoidAgv.getRobotSn();
            String avoidPos = "LM250";
            RobotApplyGotoTargetDto applyDto = new RobotApplyGotoTargetDto();
            applyDto.setClientId(clientId);
            applyDto.setCurrentPos(avoidAgv.getCurrentPos());
            applyDto.setTargetPos(avoidAgv.getTargetPos());
            LinkedHashMap<String, CheckOperationFunction> middlePos = new LinkedHashMap<>();
            middlePos.put(avoidPos, () -> {
                try {
                    List<String> posList = List.of("LM231", "LM55");
                    String otherPos = this.getOtherAgvPos(clientId);
                    while (posList.contains(otherPos)) {
                        otherPos = this.getOtherAgvPos(clientId);
                        TimeUnit.SECONDS.sleep(1);
                    }
                } catch (Exception e) {
                    log.error(e.getMessage(), e);
                }
            });
            applyDto.setMiddlePos(middlePos);
            //重新下发路径资源申请
            SpringUtil.getApplicationContext().publishEvent(applyDto);
        }
    }


    //资源101 (LM33,LM183,LM182,LM181) 和 资源66 (LM249,LM234,LM233,LM222,LM190) 存在AGV相互申请
    private void handleAgvBlock101With66(List<HandleAgvBlockDto> list) {
        Map<String, HandleAgvBlockDto> map = list.stream()
                .collect(Collectors.toMap(HandleAgvBlockDto::getCurrentPos, v -> v));
        HandleAgvBlockDto avoidAgv = map.get("LM33");
        HandleAgvBlockDto otherAgv = map.get("LM190");
        if (avoidAgv != null) {//AGV前往LM188避让，直到其他AGV离开资源101和64
            String clientId = avoidAgv.getRobotSn();
            String avoidPos = "LM188";
            RobotApplyGotoTargetDto applyDto = new RobotApplyGotoTargetDto();
            applyDto.setClientId(clientId);
            applyDto.setCurrentPos(avoidAgv.getCurrentPos());
            applyDto.setTargetPos(avoidAgv.getTargetPos());
            LinkedHashMap<String, CheckOperationFunction> middlePos = new LinkedHashMap<>();
            middlePos.put(avoidPos, () -> {
                try {
                    //LM190,LM33
                    List<String> posList = List.of("LM190", "LM33");
                    String otherPos = this.getOtherAgvPos(clientId);
                    while (posList.contains(otherPos)) {
                        otherPos = this.getOtherAgvPos(clientId);
                        TimeUnit.SECONDS.sleep(1);
                    }
                } catch (Exception e) {
                    log.error(e.getMessage(), e);
                }
            });
            applyDto.setMiddlePos(middlePos);
            //重新下发路径资源申请
            SpringUtil.getApplicationContext().publishEvent(applyDto);
        }
    }

    //资源84 (LM202,LM210,LM236,LM29,LM235,LM212,LM28,LM54) 和
    //资源24 (LM203,LM207,LM208,LM209,LM218,LM57,LM242,LM243,LM244,LM245,LM246,LM61,LM60,LM215,LM216,LM58,LM225)
    //存在AGV相互申请
    private void handleAgvBlock84With24(List<HandleAgvBlockDto> list) {
        Map<String, HandleAgvBlockDto> map = list.stream()
                .collect(Collectors.toMap(HandleAgvBlockDto::getCurrentPos, v -> v));
        HandleAgvBlockDto avoidAgv = map.get("LM236");
        HandleAgvBlockDto otherAgv = map.get("LM225");
        if (avoidAgv != null) {//AGV前往LM5避让，直到其他AGV离开资源84和24
            String clientId = avoidAgv.getRobotSn();
            String avoidPos = "LM5";
            RobotApplyGotoTargetDto applyDto = new RobotApplyGotoTargetDto();
            applyDto.setClientId(clientId);
            applyDto.setCurrentPos(avoidAgv.getCurrentPos());
            applyDto.setTargetPos(avoidAgv.getTargetPos());
            LinkedHashMap<String, CheckOperationFunction> middlePos = new LinkedHashMap<>();
            middlePos.put(avoidPos, () -> {
                try {
                    List<String> posList = List.of("LM225", "LM236", "LM29", "LM235");
                    String otherPos = this.getOtherAgvPos(clientId);
                    while (posList.contains(otherPos)) {
                        otherPos = this.getOtherAgvPos(clientId);
                        TimeUnit.SECONDS.sleep(1);
                    }
                } catch (Exception e) {
                    log.error(e.getMessage(), e);
                }
            });
            applyDto.setMiddlePos(middlePos);
            //重新下发路径资源申请
            SpringUtil.getApplicationContext().publishEvent(applyDto);
        }
    }

    //资源244 (LM31) 和 资源84 (LM202,LM210,LM236,LM29,LM235,LM212,LM28,LM54) 存在AGV相互申请
    private void handleAgvBlock244With84(List<HandleAgvBlockDto> list) {

        Map<String, HandleAgvBlockDto> map = list.stream()
                .collect(Collectors.toMap(HandleAgvBlockDto::getCurrentPos, v -> v));
        HandleAgvBlockDto avoidAgv = map.get("LM31");
        HandleAgvBlockDto otherAgv = map.get("LM54");
        if (avoidAgv != null) {//AGV前往avoidPos避让，直到其他AGV离开资源244和84
            String clientId = avoidAgv.getRobotSn();
            String avoidPos = "LM191";
            //其他AGV前往待命点 或者 报废轴/新轴/旧轴下线位
            if (List.of("LM1", "LM34", "LM22", "LM23", "LM25").contains(otherAgv.getTargetPos())) {
                avoidPos = "LM32";
            }
            RobotApplyGotoTargetDto applyDto = new RobotApplyGotoTargetDto();
            applyDto.setClientId(clientId);
            applyDto.setCurrentPos(avoidAgv.getCurrentPos());
            applyDto.setTargetPos(avoidAgv.getTargetPos());
            LinkedHashMap<String, CheckOperationFunction> middlePos = new LinkedHashMap<>();
            middlePos.put(avoidPos, () -> {
                try {
                    List<String> posList = List.of("LM54", "LM31");
                    String otherPos = this.getOtherAgvPos(clientId);
                    while (posList.contains(otherPos)) {
                        otherPos = this.getOtherAgvPos(clientId);
                        TimeUnit.SECONDS.sleep(1);
                    }
                } catch (Exception e) {
                    log.error(e.getMessage(), e);
                }
            });
            applyDto.setMiddlePos(middlePos);
            //重新下发路径资源申请
            SpringUtil.getApplicationContext().publishEvent(applyDto);
        }
    }

    //资源244 (LM31) 和 资源94 (LM32,LM113,LM112,LM111) 存在AGV相互申请
    private void handleAgvBlock244With94(List<HandleAgvBlockDto> list) {
        Map<String, HandleAgvBlockDto> map = list.stream()
                .collect(Collectors.toMap(HandleAgvBlockDto::getCurrentPos, v -> v));
        HandleAgvBlockDto avoidAgv = map.get("LM31");
        HandleAgvBlockDto otherAgv = map.get("LM32");
        if (avoidAgv != null) {//AGV前往avoidPos避让，直到其他AGV离开资源244和94
            String clientId = avoidAgv.getRobotSn();
            String avoidPos = "LM54";
            //其他AGV前往 报废轴下线位 或者 新/旧轴上线位
            if (List.of("LM29", "LM57").contains(otherAgv.getTargetPos())) {
                avoidPos = "LM191";
            }
            RobotApplyGotoTargetDto applyDto = new RobotApplyGotoTargetDto();
            applyDto.setClientId(clientId);
            applyDto.setCurrentPos(avoidAgv.getCurrentPos());
            applyDto.setTargetPos(avoidAgv.getTargetPos());
            LinkedHashMap<String, CheckOperationFunction> middlePos = new LinkedHashMap<>();
            middlePos.put(avoidPos, () -> {
                try {
                    List<String> posList = List.of("LM32", "LM31");
                    String otherPos = this.getOtherAgvPos(clientId);
                    while (posList.contains(otherPos)) {
                        otherPos = this.getOtherAgvPos(clientId);
                        TimeUnit.SECONDS.sleep(1);
                    }
                } catch (Exception e) {
                    log.error(e.getMessage(), e);
                }
            });
            applyDto.setMiddlePos(middlePos);
            //重新下发路径资源申请
            SpringUtil.getApplicationContext().publishEvent(applyDto);
        }
    }

    //资源244 (LM31) 和 资源234 (LM191,LM4,LM240,LM26) 存在AGV相互申请
    private void handleAgvBlock244With234(List<HandleAgvBlockDto> list) {
        Map<String, HandleAgvBlockDto> map = list.stream()
                .collect(Collectors.toMap(HandleAgvBlockDto::getCurrentPos, v -> v));
        HandleAgvBlockDto avoidAgv = map.get("LM31");
        HandleAgvBlockDto otherAgv = map.get("LM191");
        if (avoidAgv != null) {//AGV前往avoidPos避让，直到其他AGV离开资源244和234
            String clientId = avoidAgv.getRobotSn();
            String avoidPos = "LM54";
            //其他AGV前往 报废轴下线位 或者 新/旧轴上线位
            if (List.of("LM29", "LM57").contains(otherAgv.getTargetPos())) {
                avoidPos = "LM32";
            }
            RobotApplyGotoTargetDto applyDto = new RobotApplyGotoTargetDto();
            applyDto.setClientId(clientId);
            applyDto.setCurrentPos(avoidAgv.getCurrentPos());
            applyDto.setTargetPos(avoidAgv.getTargetPos());
            LinkedHashMap<String, CheckOperationFunction> middlePos = new LinkedHashMap<>();
            middlePos.put(avoidPos, () -> {
                try {
                    List<String> posList = List.of("LM191", "LM31");
                    String otherPos = this.getOtherAgvPos(clientId);
                    while (posList.contains(otherPos)) {
                        otherPos = this.getOtherAgvPos(clientId);
                        TimeUnit.SECONDS.sleep(1);
                    }
                } catch (Exception e) {
                    log.error(e.getMessage(), e);
                }
            });
            applyDto.setMiddlePos(middlePos);
            //重新下发路径资源申请
            SpringUtil.getApplicationContext().publishEvent(applyDto);
        }
    }

    //资源196 (LM24) 和 资源234 (LM191,LM4,LM240,LM26) 存在AGV相互申请
    private void handleAgvBlock196With234(List<HandleAgvBlockDto> list) {
        Map<String, HandleAgvBlockDto> map = list.stream()
                .collect(Collectors.toMap(HandleAgvBlockDto::getCurrentPos, v -> v));
        HandleAgvBlockDto avoidAgv = map.get("LM26");
        if (avoidAgv != null) {//AGV改变路径，前往LM27->LM25中间点
            String clientId = avoidAgv.getRobotSn();
            RobotApplyGotoTargetDto applyDto = new RobotApplyGotoTargetDto();
            applyDto.setClientId(clientId);
            applyDto.setCurrentPos(avoidAgv.getCurrentPos());
            applyDto.setTargetPos(avoidAgv.getTargetPos());
            LinkedHashMap<String, CheckOperationFunction> middlePos = new LinkedHashMap<>();
            middlePos.put("LM27", null);
            middlePos.put("LM25", null);
            applyDto.setMiddlePos(middlePos);
            //重新下发路径资源申请
            SpringUtil.getApplicationContext().publishEvent(applyDto);
        }
    }

    //资源196 (LM24) 和 资源216 (LM25) 存在AGV相互申请
    private void handleAgvBlock196With216(List<HandleAgvBlockDto> list) {
        Map<String, HandleAgvBlockDto> map = list.stream()
                .collect(Collectors.toMap(HandleAgvBlockDto::getCurrentPos, v -> v));
        HandleAgvBlockDto avoidAgv = map.get("LM25");
        if (avoidAgv != null) {//AGV改变路径，前往LM27->LM26中间点
            String clientId = avoidAgv.getRobotSn();
            RobotApplyGotoTargetDto applyDto = new RobotApplyGotoTargetDto();
            applyDto.setClientId(clientId);
            applyDto.setCurrentPos(avoidAgv.getCurrentPos());
            applyDto.setTargetPos(avoidAgv.getTargetPos());
            LinkedHashMap<String, CheckOperationFunction> middlePos = new LinkedHashMap<>();
            middlePos.put("LM27", null);
            middlePos.put("LM26", null);
            applyDto.setMiddlePos(middlePos);
            //重新下发路径资源申请
            SpringUtil.getApplicationContext().publishEvent(applyDto);
        }
    }

    //资源194 (LM196,LM195,LM22) 和 资源214 (LM199,LM198,LM23) 存在AGV相互申请
    private void handleAgvBlock194With214(List<HandleAgvBlockDto> list) {
        Map<String, HandleAgvBlockDto> map = list.stream()
                .collect(Collectors.toMap(HandleAgvBlockDto::getCurrentPos, v -> v));
        HandleAgvBlockDto avoidAgv = map.get("LM23");
        if (avoidAgv != null) {//AGV改变路径，前往LM25->LM24中间点
            String clientId = avoidAgv.getRobotSn();
            RobotApplyGotoTargetDto applyDto = new RobotApplyGotoTargetDto();
            applyDto.setClientId(clientId);
            applyDto.setCurrentPos(avoidAgv.getCurrentPos());
            applyDto.setTargetPos(avoidAgv.getTargetPos());
            LinkedHashMap<String, CheckOperationFunction> middlePos = new LinkedHashMap<>();
            middlePos.put("LM25", null);
            middlePos.put("LM24", null);
            applyDto.setMiddlePos(middlePos);
            //重新下发路径资源申请
            SpringUtil.getApplicationContext().publishEvent(applyDto);
        }
    }

    //资源214 (LM199,LM198,LM23) 和 资源216 (LM25) 存在AGV相互申请
    private void handleAgvBlock214With216(List<HandleAgvBlockDto> list) {
        Map<String, HandleAgvBlockDto> map = list.stream()
                .collect(Collectors.toMap(HandleAgvBlockDto::getCurrentPos, v -> v));
        HandleAgvBlockDto avoidAgv = map.get("LM25");
        if (avoidAgv != null) {//AGV前往LM27避让，直到其他AGV离开资源214和216
            String clientId = avoidAgv.getRobotSn();
            String avoidPos = "LM27";
            RobotApplyGotoTargetDto applyDto = new RobotApplyGotoTargetDto();
            applyDto.setClientId(clientId);
            applyDto.setCurrentPos(avoidAgv.getCurrentPos());
            applyDto.setTargetPos(avoidAgv.getTargetPos());
            LinkedHashMap<String, CheckOperationFunction> middlePos = new LinkedHashMap<>();
            middlePos.put(avoidPos, () -> {
                try {
                    List<String> posList = List.of("LM199", "LM198", "LM23", "LM25");
                    String otherPos = this.getOtherAgvPos(clientId);
                    while (posList.contains(otherPos)) {
                        otherPos = this.getOtherAgvPos(clientId);
                        TimeUnit.SECONDS.sleep(1);
                    }
                } catch (Exception e) {
                    log.error(e.getMessage(), e);
                }
            });
            applyDto.setMiddlePos(middlePos);
            //重新下发路径资源申请
            SpringUtil.getApplicationContext().publishEvent(applyDto);
        }
    }

    //资源194 (LM196,LM195,LM22) 和 资源196 (LM24) 存在AGV相互申请
    private void handleAgvBlock194With196(List<HandleAgvBlockDto> list) {
        Map<String, HandleAgvBlockDto> map = list.stream()
                .collect(Collectors.toMap(HandleAgvBlockDto::getCurrentPos, v -> v));
        HandleAgvBlockDto avoidAgv = map.get("LM24");
        if (avoidAgv != null) {//AGV前往LM25避让，直到其他AGV离开资源194和196
            String clientId = avoidAgv.getRobotSn();
            String avoidPos = "LM25";
            RobotApplyGotoTargetDto applyDto = new RobotApplyGotoTargetDto();
            applyDto.setClientId(clientId);
            applyDto.setCurrentPos(avoidAgv.getCurrentPos());
            applyDto.setTargetPos(avoidAgv.getTargetPos());
            LinkedHashMap<String, CheckOperationFunction> middlePos = new LinkedHashMap<>();
            middlePos.put(avoidPos, () -> {
                try {
                    List<String> posList = List.of("LM196", "LM195", "LM22", "LM24");
                    String otherPos = this.getOtherAgvPos(clientId);
                    while (posList.contains(otherPos)) {
                        otherPos = this.getOtherAgvPos(clientId);
                        TimeUnit.SECONDS.sleep(1);
                    }
                } catch (Exception e) {
                    log.error(e.getMessage(), e);
                }
            });
            applyDto.setMiddlePos(middlePos);
            //重新下发路径资源申请
            SpringUtil.getApplicationContext().publishEvent(applyDto);
        }
    }

}